package com.shop.front.dao.user;


import com.shop.front.request.user.PageRecommendReq;
import com.shop.front.request.user.StatPromotersReq;
import com.shop.front.vo.user.UserPromotersVo;
import com.shop.domain.user.User;
import com.shop.jpa.BaseSQLDao;
import com.shop.jpa.support.QuerySQL;
import com.shop.web.context.WebContext;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Repository;

@Repository
public class UserSQLDao extends BaseSQLDao {

    /**
     * 分页查询推荐用户列表
     *
     * @param userId
     * @param req
     * @return
     */
    public Page<User> pageRecommend(Long userId, PageRecommendReq req) {
        StringBuffer field = new StringBuffer("u.*");
        String from = "t_user u";
        String order = "u.id desc";
        QuerySQL qs = new QuerySQL(WebContext.getPageNo(), WebContext.getPageSize(), field.toString(), from, order);
        qs.addWhereWithoutParams("u.is_deleted = 0");
        qs.addWhereEq("u.pid", userId);
        if(null == req.getHaveApplyCredit()) {
            req.setHaveApplyCredit(1);
        }
        qs.addWhereLikeNotBlank("u.mobile", req.getMobile());
        qs.addWhereEq("u.have_apply_credit", req.getHaveApplyCredit());
        return this.page(qs, User.class);
    }

    public Page<UserPromotersVo> queryPromoterPage(Long userId, StatPromotersReq statPromotersReq){

        StringBuffer field = new StringBuffer("u.id,u.account,u.avatar,u.create_time");
        String from = "t_user u";
        String order = "u.create_time desc";
        QuerySQL qs = new QuerySQL(WebContext.getPageNo(), WebContext.getPageSize(), field.toString(), from, order);
        qs.addWhereWithoutParams("u.is_deleted = 0");
        if(statPromotersReq.getType() == 0){//一级
            qs.addWhereEq("u.spread_uid", userId);

        }else{
            qs.addWhereParams(" EXISTS (select 1 from t_user ru where u.spread_uid = ru.id and ru.spread_uid = ? )", userId);
            //qs.addWhereParams(" EXISTS (select 1 from t_user ru where u.spread_uid = ru.id and ru.spread_uid = ? and ru.spreadids is not null )", userId);
        }
        //qs.addWhereWithoutParams("u.spreadids is not null");
        qs.addWhereLikeNotBlank("u.mobile", statPromotersReq.getSeach());

        return this.page(qs, UserPromotersVo.class);
    }

}
